package com.cnfangmao.masterdata.controller;


import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.BasicDataMetadataDto;
import com.cnfangmao.masterdata.dto.BasicDataMetadataSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataFieldMetadata;
import com.cnfangmao.masterdata.model.BasicDataMetadata;
import com.cnfangmao.masterdata.service.BasicDataFieldMetadataService;
import com.cnfangmao.masterdata.service.BasicDataMetadataService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 主数据元数据 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@RequestMapping("/basicDataMetadata")
@Api(tags = "1-0、配置-主数据元数据管理")
@RestController
public class BasicDataMetadataController {
	
	private static final Logger logger = LoggerFactory.getLogger(BasicDataMetadataController.class);

	@Autowired
	private BasicDataMetadataService basicDataMetadataService;
	
	@Autowired
	private BasicDataFieldMetadataService basicDataFieldMetadataService;

	/**
	 * 主数据元数据列表
	 */
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ApiOperation(value = "主数据元数据列表", notes = "")
	@ApiImplicitParam(name = "dto", value = "主数据元数据列表查询条件", required = true, paramType = "body", dataType = "GeneralSearchDto«BasicDataMetadataSearchDto»")
	public ResBody<GeneralPageVo<BasicDataMetadata>>  listBasicDataMetadata(@RequestBody GeneralSearchDto<BasicDataMetadataSearchDto> dto) {
		ResBody<GeneralPageVo<BasicDataMetadata>>  resBody = new ResBody<GeneralPageVo<BasicDataMetadata>>();
		try {
			IPage<BasicDataMetadata> data = basicDataMetadataService.queryListBasicDataMetadata(dto);
			GeneralPageVo<BasicDataMetadata> result= new GeneralPageVo<BasicDataMetadata>();
			result.setPageNo(data.getCurrent());
			result.setPageSize(data.getPages());
			result.setRecords(data.getRecords());
			result.setTotalCount(data.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in BasicDataMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 创建主数据元数据
	 */
	@RequestMapping(value = "/create.do", method = RequestMethod.POST)
	@ApiOperation(value = "创建主数据元数据", notes = "")
	@ApiImplicitParam(name = "input", value = "创建主数据元数据信息", required = true, paramType = "body", dataType = "BasicDataMetadataDto")
	public ResBody<Boolean> createBasicDataMetadata(@Validated @RequestBody @NotNull BasicDataMetadataDto input) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			if (input.getMetadataName().isEmpty()) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("主数据元数据名称不能空");
				return resBody;
			}
			if (basicDataMetadataService.findByMetadataName(input.getMetadataName()) != null) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("主数据元数据名称已经存在");
				return resBody;
			}
			if (basicDataMetadataService.findByMetadataCode(input.getMetadataCode()) != null) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("主数据元数据编码已经存在");
				return resBody;
			}
			input.setMetadataId(SnowFlakeUtils.generator());
			BasicDataMetadata  basicDataMetadata = ModelUtils.parseObject(input, BasicDataMetadata.class, "create");
			basicDataMetadataService.save(basicDataMetadata);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(true);
			resBody.message("创建成功");	
		} catch (Exception e) {
			logger.error(String.format("run list create in BasicDataMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * 更新主数据元数据信息
	 */
	@RequestMapping(value = "/update.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新主数据元数据", notes = "")
	@ApiImplicitParam(name = "input", value = "更新主数据元数据信息", required = true, paramType = "body", dataType = "BasicDataMetadataDto")
	public ResBody<Boolean> updateBasicDataMetadata(@Validated @RequestBody @NotNull BasicDataMetadataDto input) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			if (input.getMetadataName().isEmpty() || input.getMetadataCode().isEmpty()) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("主数据元数据名称或者主数据元数据编码为空");
				return resBody;
			}
			BasicDataMetadata metadataName= basicDataMetadataService.findByMetadataName(input.getMetadataName());
			if (metadataName != null && !metadataName.getMetadataId().equals(input.getMetadataId())) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("主数据元数据名称已经存在");
				return resBody;
			}
			BasicDataMetadata metadataCode= basicDataMetadataService.findByMetadataCode(input.getMetadataCode());
			if (metadataCode != null && !metadataCode.getMetadataId().equals(input.getMetadataId())) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("主数据元数据编码已经存在");
				return resBody;
			}
			BasicDataMetadata entity =ModelUtils.parseObject(input, BasicDataMetadata.class, "update");
			basicDataMetadataService.updateById(entity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(true);
			resBody.message("修改成功");
		} catch (Exception e) {
			logger.error(String.format("run list update in BasicDataMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		
		return resBody;
	}

	/**
	 * 删除主数据元数据
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除主数据元数据", notes = "")
	@ApiImplicitParam(name = "metadataId", value = "主数据元数据metadataId",paramType = "body", required = true, dataType = "string")
	public ResBody<Boolean> deleteBasicDataMetadata(@Validated @RequestBody @NotNull String metadataId) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			basicDataMetadataService.removeById(metadataId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(true);
			resBody.message("删除成功");
		} catch (Exception e) {
			logger.error(String.format("run list delete in BasicDataMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	/**
     * 获取主数据元数据详情
     */
    @RequestMapping(value="/getBasicDataMetadata.do", method = RequestMethod.POST)
    @ApiOperation(value="获取主数据元数据详情", notes = "")
    @ApiImplicitParam(name = "metadataId", value = "主数据元数据metadataId", required = true, paramType = "body",dataType = "string")
    public ResBody<BasicDataMetadata> getBasicDataMetadata(@Validated @RequestBody @NotNull String metadataId){
    	ResBody<BasicDataMetadata> resBody = new ResBody<BasicDataMetadata>();
    	try {
        	BasicDataMetadata basicDataMetadata=basicDataMetadataService.getById(metadataId);
          	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(basicDataMetadata);
		} catch (Exception e) {
			logger.error(String.format("run list getBasicDataMetadata in BasicDataMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }
    
    /**
	 * 根据主数据元数据metadataId获取主数据元数据列表
	 */
	@RequestMapping(value = "/getBasicDataFieldMetadataByMetadataId.do", method = RequestMethod.POST)
	@ApiOperation(value = "根据主数据元数据metadataId获取字段元数据列表", notes = "")
	@ApiImplicitParam(name = "metadataId", value = "主数据元数据metadataId", required = true, paramType = "body",dataType = "string")
	public ResBody<List<BasicDataFieldMetadata>>  getBasicDataFieldMetadataByMetadataId(@RequestBody String metadataId) {
		ResBody<List<BasicDataFieldMetadata>>  resBody = new ResBody<List<BasicDataFieldMetadata>>();
		try {
			List<BasicDataFieldMetadata> data = basicDataFieldMetadataService.findByFieldMetadataId(metadataId);
          	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(data);
		} catch (Exception e) {
			logger.error(String.format("run getBasicDataFieldMetadataByMetadataId error in BasicDataMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

}

